package com.libre.view.file;

import com.libre.seal.SealUtils;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.control.*;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;

public class SealTab {

   static Toggle toggle;

    public GridPane getGridPane(){
        GridPane gridPane = new GridPane();
        //水平和垂直间距
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        // 图片
        ImageView imageView = new ImageView( "./seal/公章1.png");
        imageView.setFitWidth(250);
        imageView.setFitHeight(250);
        gridPane.add(imageView, 0, 2,2,1);

        // 内容
        Label content = new Label("内容:");
        gridPane.add(content, 0, 3);
        TextField textField = new TextField();
        textField.setPromptText("请输入印章信息");
        gridPane.add(textField, 1, 3);

        // 选项卡
        HBox hBox = new HBox();
        ToggleGroup group1 = new ToggleGroup();
        RadioButton button1 = new RadioButton("公章1");
        button1.setToggleGroup(group1);
        toggle = button1;
        button1.setSelected(true);
        RadioButton button2 = new RadioButton("公章2");
        button2.setToggleGroup(group1);
        RadioButton button3 = new RadioButton("合同专用章");
        button3.setToggleGroup(group1);
        RadioButton button4 = new RadioButton("私章1");
        button4.setToggleGroup(group1);
        RadioButton button5 = new RadioButton("私章2");
        button5.setToggleGroup(group1);

        group1.selectedToggleProperty().addListener(new ChangeListener<Toggle>() {
            @Override
            public void changed(ObservableValue<? extends Toggle> observable, Toggle oldValue, Toggle newValue) {
                toggle = newValue;
                if (toggle == button1) {
                    imageView.setImage(new Image("./seal/公章1.png"));
                } else if (toggle == button2) {
                    imageView.setImage(new Image("./seal/公章2.png"));
                } else if (toggle == button3) {
                    imageView.setImage(new Image("./seal/公章3.png"));
                } else if (toggle ==  button4) {
                    imageView.setImage(new Image("./seal/私章1.png"));
                } else if (toggle == button5) {
                    imageView.setImage(new Image("./seal/私章2.png"));
                }
            }
        });
        hBox.getChildren().addAll(button1,button2,button3,button4,button5 );
        gridPane.add(hBox, 0, 4,2,1);

        // 生成
        Button button = new Button("生成");
        button.setOnAction(event->{
            try {
                if (toggle == button1) {
                    String s = SealUtils.OfficialSeal_1(textField.getText());
                    imageView.setImage(new Image(s));
                } else if (toggle == button2) {
                    String s = SealUtils.OfficialSeal_2(textField.getText());
                    imageView.setImage(new Image(s));
                } else if (toggle == button3) {
                    String s = SealUtils.OfficialSeal_3(textField.getText());
                    imageView.setImage(new Image(s));
                } else if (toggle ==  button4) {
                    String s = SealUtils.PrivateSeal_1(textField.getText());
                    imageView.setImage(new Image(s));
                } else if (toggle == button5) {
                    String s = SealUtils.PrivateSeal_2(textField.getText());
                    imageView.setImage(new Image(s));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        });


        gridPane.add(button, 1, 10,2,1);

        return gridPane;
    }

}
